const INFO_KEY = 'vue-admin'
const expiredTime = 1000 * 60 * 60 * 24

// const localData = {
//   timestamp: Date.now(),
//   token: '1324dafa',
// };

// 获取用户信息
export function getUser() {
  try {
    const { timestamp, userInfo } = JSON.parse(localStorage.getItem(INFO_KEY))
    if (timestamp + expiredTime > Date.now()) {
      return userInfo
    } else {
      return null
    }
  } catch (e) {
    return null
  }
}

// 储存用户信息
export function saveUser(userInfo) {
  const localData = {
    userInfo,
    timestamp: Date.now()
  }
  localStorage.setItem(INFO_KEY, JSON.stringify(localData))
}

// 获取token
export function getToken() {
  const userInfo = getUser()
  return userInfo ? userInfo.token : null
}

// 清空token
export function clearAuth() {
  localStorage.clear()
}
